<?php

/**
 * This is the model class for table "{{positions}}".
 *
 * The followings are the available columns in table '{{positions}}':
 * @property integer $zpo_id
 * @property string $zpo_titile
 * @property string $zpo_type
 * @property integer $zna_id
 * @property integer $zpo_maxnum
 */
class Positions extends CActiveRecord
{
	public $type = array(
			1=>'产品',
			2=>'资讯',
			3=>'团购',
			4=>'限时折扣',
			5=>'抢购秒杀',
			);
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Positions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{positions}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zna_id, zpo_maxnum', 'numerical', 'integerOnly'=>true),
			array('zpo_title', 'length', 'max'=>255),
			array('zpo_type', 'length', 'max'=>1),
			array('zna_id', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpo_id, zpo_title, zpo_type, zna_id, zpo_maxnum', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'data'=>array(self::HAS_MANY, 'PosLog', array('zpo_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpo_id' => 'Zpo',
			'zpo_title' => '推荐位标题',
			'zpo_type' => '推荐位类型',
			'zna_id' => '所在页面',
			'zpo_maxnum' => '最大推荐条数',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpo_id',$this->zpo_id);
		$criteria->compare('zpo_title',$this->zpo_title,true);
		$criteria->compare('zpo_type',$this->zpo_type,true);
		$criteria->compare('zna_id',$this->zna_id);
		$criteria->compare('zpo_maxnum',$this->zpo_maxnum);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>100,
					)
		));
	}

	/**
	 * 根据菜单id和推荐位类型获取推荐位
	 * $id 菜单id
	 * $type 类型 1产品 2资讯 3团购
	 * @return CActiveDataProvider
	 */
	public function listSearch($id,$type)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('zpo_type',$type);
		$criteria->compare('zna_id',$id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>100,
			)
		));
	}

	/**
	 * 根据推荐位id获取内容
	 * $id 推荐位id
	 * $pic 是否必须有图片
	 * @return Array
	 */
	public function getPosData( $id, $amount = null,$pic = false )
	{
		$criteria=new CDbCriteria;

		$criteria->compare('zpo_id',$id);
		$criteria->compare('zpl_start_time','<'.time());
		$criteria->compare('zpl_end_time','>'.time());
		$criteria->order = 'zpl_start_time DESC';
		if((int)$amount){
			$criteria->limit = $amount;			
		}
		if( $pic ){
			$criteria->addCondition('zpl_pic IS NOT NULL');
		}
		return PosLog::model()->findAll($criteria);
	}

	/**
	 * 根据菜单id(页面)和推荐位类型获取推荐位
	 * $id 菜单id
	 * $type 类型 1产品 2资讯 3团购
	 * @return Array
	 */
	public function getPositionsByPage($id,$type)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('zpo_type',$type);
		$criteria->compare('zna_id',$id);

		return $this->findAll($criteria);
	}
}